package parkManage;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * 车辆信息
 * 
 * @author 伟❤志
 *
 */
public class CarModel {

	private String license;// 车牌号：不可修改
	private double hight;// 车辆高度：不可修改
	private byte isPay;// 是否缴费->未缴费不可离开，可用作判断是否离开
	private Date startTime;// 计时
	private Date leaveTime;// 离开时间
	private int positionId;// 车位号

	static Scanner scanner = new Scanner(System.in);

	public String getLicense() {
		return license;
	}

	public void setLicense(String license) {
		this.license = license;
	}

	public double getHight() {
		return hight;
	}

	public void setHight(double hight) {
		this.hight = hight;
	}

	public byte getIsPay() {
		return isPay;
	}

	public void setIsPay(byte isPay) {
		this.isPay = isPay;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getLeaveTime() {
		return leaveTime;
	}

	public void setLeaveTime(Date leaveTime) {
		this.leaveTime = leaveTime;
	}

	public int getPositionId() {
		return positionId;
	}

	public void setPositionId(int positionId) {
		this.positionId = positionId;
	}

	/**
	 * 
	 * --根据车牌号查id
	 * 
	 * @param cars
	 * @param positions
	 * @param license
	 */
	public int selectCar(String license) {
		for (int i = 0; i < ParkTest.cars.size(); i++) {
			if (license.equals(ParkTest.cars.get(i).getLicense())) {
				return i;
			}
		}
		return -1;
	}

	/**
	 * 查询车辆详细信息
	 * 
	 * @param cars
	 * @param positions
	 * @param id
	 */
	public void carDetails() {
		CarModel carModel = new CarModel();

		System.out.println("输入车牌号：");
		String lic = scanner.nextLine();

		int id = carModel.selectCar(lic);

		if (id == -1) {
			System.out.println("没有该车辆信息！");
			return;
		}
		System.out.println("车牌号：" + ParkTest.cars.get(id).getLicense());
		System.out.println("车辆高度：" + ParkTest.cars.get(id).getHight() + "米");
		// System.out.println("产生费用" + positions.get(id).getCost());
		String isPay = ParkTest.cars.get(id).getIsPay() == 1 ? "已付费" : "未付费";
		System.out.println("是否付费：" + isPay);

		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern("yyyy-MM-dd HH:mm"); // 格式化时间
		System.out.println("进入停车场时间：" + sdf.format(ParkTest.cars.get(id).getStartTime()));
		int temp = ParkTest.cars.get(id).getIsPay();
		System.out.println("离开停车场时间：" + (temp == 1 ? (sdf.format(ParkTest.cars.get(id).getLeaveTime())) : "未离开停车场！"));
	}

	/**
	 * 增加车辆信息 停车
	 * 
	 * @param cars
	 * @param positions
	 */
	public void addCar() {
		CarModel carModel = new CarModel();
		PositionModel positionModel = new PositionModel();

		System.out.println();

		System.out.print("请输入车牌号:");
		String license = scanner.nextLine();
		carModel.setLicense(license);// 保存车牌号
		positionModel.setCarLicense(license);

		System.out.print("请输入车辆高度:");
		double hight = Integer.parseInt(scanner.nextLine());
		if (hight > 3) {
			System.out.println("车辆高度超过最大限制，请到其他停车场停车！");
			return;
		}
		carModel.setHight(hight);

		carModel.setIsPay((byte) 0);// 车辆进入停车场将是否支付设置为未支付

		Date date = new Date();
		carModel.setStartTime(date);// 获取当前时间-》车辆进入时间

		System.out.print("请输入停放车辆车位号：");
		int id = Integer.parseInt(scanner.nextLine());
		int val = carModel.isExist(id);
		if (val == -1) {
			System.out.println("不存在这个车位号！");
			return;
		}

		ParkTest.occupy.add(id);// 将已用车位存入集合
		positionModel.setIsFree(0);// 设置空闲位置为否
		carModel.setPositionId(id);// 车位号
		positionModel.setId(id);// 车辆停放车位号

		ParkTest.cars.add(carModel);// 将车辆信息新增到集合中
		ParkTest.positions.add(positionModel);

	}

	/**
	 * 查询是否存在这个车位号
	 * 
	 * @param frees
	 */
	public int isExist(int id) {
		for (int i : ParkTest.frees) {
			if (i == id) {
				ParkTest.frees.remove(i);// 将空闲位置移除
				return i;
			}
		}
		return -1;
	}

	/**
	 * 车辆离开
	 * 
	 * @param cars
	 * @param positions
	 * @param id
	 */
	public void leaveCar() {
		PositionModel positionModel = new PositionModel();
		CarModel carModel = new CarModel();
		Date date = new Date();

		System.out.println();

		System.out.print("请输入车牌号：");
		String license = scanner.nextLine();
		int id = carModel.selectCar(license);// 根据车牌号获取车辆id
		if (id == -1) {
			System.out.println("停车场没有此车辆！");
			return;
		}

		System.out.print("请输入车位号：");
		int position = Integer.parseInt(scanner.nextLine());

		if (position != ParkTest.cars.get(id).getPositionId()) {
			System.out.println("该车辆不在此车位，请检查！");
			return;
		}

		ParkTest.cars.get(id).setLeaveTime(date);// 获取当前离开时间

		ParkTest.frees.add(position);// 增加空闲位置
		ParkTest.occupy.remove(position);

		int poId = positionModel.selectPosition(ParkTest.cars.get(id).getPositionId());
		positionModel.calculate(poId, ParkTest.cars.get(id).getLicense());

		System.out.println("请支付：" + ParkTest.positions.get(id).getCost() + "元");// 获取费用信息
		System.out.println("点击回车支付...");
		scanner.nextLine();

		ParkTest.cars.get(id).setIsPay((byte) 1);// 将是否支付设置为已支付

		// carModel.carDetails(cars, positions, id);

		ParkModel parkModel = new ParkModel();
		parkModel.addCost(license, position);
	}

}
